package com.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.SaleVisitDao;
import com.crm.domain.PageBean;
import com.crm.domain.SaleVisit;
import com.crm.service.SaleVisitService;
/**
 * @author Yusei
 * 客户拜访记录Service接口实现类
 * 记得要添加事务注解，不然会报错
 */

@Transactional
public class SaleVisitServiceImpl implements SaleVisitService {
	// 注入saleVisitDao
	@Resource(name="saleVisitDao")
	private SaleVisitDao saleVisitDao;

	@Override
	public void print() {
		saleVisitDao.print();
	}

	// 查询所有（带分页查询）
	@Override
	public PageBean<SaleVisit> findByPage(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
		// 设置参数
		// 初始化页面容器
		PageBean<SaleVisit> pageBean = new PageBean<SaleVisit>();
		// 设置当前页
		pageBean.setCurrPage(currPage);
		// 设置页面大小
		pageBean.setPageSize(pageSize);
		// 设置总记录数
		Integer totalCount = saleVisitDao.findCount(detachedCriteria);
		pageBean.setTotalCount(totalCount);
		// 设置总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());
		// 设置查询的起始位置（当前页-1 乘以页面大小）
		Integer begin = (currPage - 1) * pageSize;
		// 将查询得到的结果放到pageBean
		List<SaleVisit> list = saleVisitDao.findByPage(detachedCriteria, begin, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	// 保存客户拜访记录
	public void save(SaleVisit saleVisit) {
		saleVisitDao.save(saleVisit);
	}
	
}
